//
//  WelfareIntegralRecordViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/8/9.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "WelfareIntegralRecordViewController.h"
#import "IntegralContentTableViewCell.h"
#import "IntergralRecordModel.h"
#import "HYSegmentControl.h"

static NSString *recordCellID = @"recordCellID";

@interface WelfareIntegralRecordViewController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *recordTableView;

@property (assign, nonatomic) NSInteger page;

@property (strong, nonatomic) NSMutableArray *recordListArr;

@end

@implementation WelfareIntegralRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WhiteColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"兑换记录";
    
 

    [self.view addSubview:self.recordTableView];
    
    __weak typeof(self) weakSelf = self;
    [self.recordTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
    }];
    
    self.page = 1;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [header beginRefreshing];
    [header setTitle:@"下拉可以刷新!" forState:MJRefreshStateIdle];
    [header setTitle:@"松手立即刷新!" forState:MJRefreshStatePulling];
    [header setTitle:@"正在加载..." forState:MJRefreshStateRefreshing];
    header.stateLabel.font = Font(24);
    header.backgroundColor = [UIColor clearColor];
    [header setTintColor:[UIColor clearColor]];
    
    header.stateLabel.textColor = Color999999;
    header.lastUpdatedTimeLabel.hidden = YES;
    self.recordTableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:@"正在加载。。。。" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多数据！" forState:MJRefreshStateNoMoreData];
    footer.stateLabel.font = Font(17);
    footer.stateLabel.textColor = Color999999;
    self.recordTableView.mj_footer = footer;
    self.recordTableView.mj_footer.hidden = NO;

}




- (void)loadNewData{
    
    [self.recordTableView.mj_footer resetNoMoreData];
    
    self.page = 1;
    [self loadData];
    
    // 刷新表格
    [self.recordTableView reloadData];
    
    // 拿到当前的上拉刷新控件，结束刷新状态
    [self.recordTableView.mj_header endRefreshing];
    
}

- (void)loadMoreData{
    
    self.page++;
    [self loadData];
    
    // 刷新表格
    [self.recordTableView reloadData];
    
    // 拿到当前的上拉刷新控件，结束刷新状态
    [self.recordTableView.mj_footer endRefreshing];
    
}

- (void)loadData {
    
    __weak typeof(self) weakSelf = self;
    [IntergralRecordModel integralRecordListWithPage:self.page successBlock:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSArray *arr = responseObject[@"data"];
            
            if (arr.count > 0 || weakSelf.recordListArr.count > 0) {
                
                self.recordTableView.hidden = NO;
                //                weakSelf.tipView.hidden = YES;
                
                if (self.page == 1) {
                    
                    weakSelf.recordListArr = (NSMutableArray*)arr;
                    
                }else {
                    
                    [weakSelf.recordListArr addObjectsFromArray:arr];
                    
                }
                
                if (self.page > 1 && arr.count == 0) {
                    
                    [self.recordTableView.mj_footer endRefreshingWithNoMoreData];
                    
                }
                // 滚动cell
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:(self.page -1) * 10];
                
                [self.recordTableView  selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
                
                [self.recordTableView reloadData];
                
            }else {
               
            }
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
        
    } failBlock:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return jisuan(140);
}

#pragma mark -delegate \ datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 设置尾部控件的显示和隐藏
    self.recordTableView.mj_footer.hidden = self.recordListArr.count == 0;
    return self.recordListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    IntegralContentTableViewCell *cell = [[IntegralContentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:recordCellID];
    cell.recordModel = self.recordListArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark -lazy
- (UITableView *)recordTableView {
    
    if (!_recordTableView) {
        _recordTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _recordTableView.delegate = self;
        _recordTableView.dataSource = self;
        _recordTableView.showsVerticalScrollIndicator = NO;
        _recordTableView.showsHorizontalScrollIndicator = NO;
        _recordTableView.estimatedRowHeight = 0;
        _recordTableView.estimatedSectionHeaderHeight = 0;
        _recordTableView.estimatedSectionFooterHeight = 0;
        _recordTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_recordTableView registerClass:[IntegralContentTableViewCell class] forCellReuseIdentifier:recordCellID];
        
        if (@available(iOS 11.0, *)) {
            _recordTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _recordTableView;
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
